const router = require("koa-router")();
const DB = require("../module/db");
const sd = require("silly-datetime");
const crypto = require("crypto");

router.get("/", async (ctx) => {
    ctx.body = "task首页";

}).post("/addTask", async (ctx) => {
    // let data = ctx.request.body
    // console.log(data)
    // ctx.body = {
    //     code: 200,
    //     status: "success",
    //     data: data
    // }

    let projectName = ctx.request.body.projectName
    let taskName = ctx.request.body.taskName
    let userName1 = ctx.request.body.userName
    let userName2 = ctx.request.body.checkinor
    let fileID = ctx.request.body.fileID
    let fileName = ctx.request.body.fileName
    let time = sd.format(new Date(), 'YYYY-MM-DD HH:mm');

    let findUser1 = await DB.find('user', {"username": userName1});
    let findUser2 = await DB.find('user', {"username": userName2});
    if (findUser1.toString().length === 0 && findUser2.toString().length === 0) {    //没有发布人和接收人
        let userData1 = {
            access_token: '',
            team: 'cgyun',
            alias: '',
            username: userName1,
            password: '',
            teamName: '',
            teamAlias: '',
            role: '',
            enable: 'true',
            createDate: new Date(),
            photoFileID: '',
            __v: '0'
        }
        let addUserResult1 = await DB.insert('user', userData1);
        let userData2 = {
            access_token: '',
            team: 'cgyun',
            alias: '',
            username: userName2,
            password: '',
            teamName: '',
            teamAlias: '',
            role: '',
            enable: 'true',
            createDate: new Date(),
            photoFileID: '',
            __v: '0'
        }
        let addUserResult2 = await DB.insert('user', userData2);

        if (addUserResult1.result.ok && addUserResult2.result.ok) {
            let project1 = {
                "name": projectName,
                "userId": addUserResult1.ops[0]._id.toString(),
                "reviews": [],
                "introduction": "欢迎来到我的新项目...",
                "collections": [],
            }
            let project2 = {
                "name": projectName,
                "userId": addUserResult2.ops[0]._id.toString(),
                "reviews": [],
                "introduction": "欢迎来到我的新项目...",
                "collections": [],
            }
            let addProjectResult1 = await DB.insert('project', project1);
            let addProjectResult2 = await DB.insert('project', project2);
            if (addProjectResult1.result.ok && addProjectResult2.result.ok) {
                let review = {
                    "name": taskName,
                    "userId": [addUserResult1.ops[0]._id.toString()],
                    "mediaId": [],
                    "introduction": "任务介绍...",
                    "link": ''
                }
                let addReviewResult1 = await DB.insert('review', review);
                let addReviewResult2 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult1.ops[0]._id.toString())}, {"reviews": addReviewResult1.ops[0]._id})
                let addReviewResult3 = await DB.update("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"link": cryptId(addReviewResult1.ops[0]._id.toString())})
                if (addReviewResult1.result.ok && addReviewResult2.result.ok && addReviewResult3.result.ok) {
                    let addCollectionResult1 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult2.ops[0]._id.toString())}, {"collections": DB.getObjectId(addReviewResult1.ops[0]._id)})
                    let addCollectionResult2 = await DB.addArray('review', {'_id': DB.getObjectId(addReviewResult1.ops[0]._id)}, {'userId': addUserResult2.ops[0]._id.toString()})
                    if (addCollectionResult1.result.ok && addCollectionResult2.result.ok) {
                        let addMediaResult1 = await DB.insert('media',
                            {
                                "userId": addUserResult1.ops[0]._id.toString(),
                                "userName": userName1,
                                "fileId": DB.getObjectId(fileID),
                                "name": fileName.split(".")[0],
                                "buffer": '',
                                "type": "image/" + fileName.split(".")[1],
                                "like": 0
                            });
                        let addMediaResult2 = await DB.addArray("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"mediaId": addMediaResult1.ops[0]._id})
                        if (addMediaResult1.result.ok && addMediaResult2.result.ok) {
                            console.log(time + "：文件" + fileName + "记录保存成功")
                            console.log(time + "：添加任务文件成功")
                            ctx.body = {
                                code: 200,
                                status: "success",
                                data: ctx.request.body
                            }
                        } else {
                            console.log(time + "：文件" + fileName + "记录保存失败")
                            ctx.body = {
                                code: 202,
                                status: "error",
                                data: time + "：文件" + fileName + "记录保存失败"
                            }
                        }
                    } else {
                        console.log(time + "：收藏任务失败");
                        ctx.body = {
                            code: 202,
                            status: "error",
                            data: time + "：收藏任务失败"
                        }
                    }
                } else {
                    console.log(time + "：添加任务失败");
                    ctx.body = {
                        code: 202,
                        status: "error",
                        data: time + "：添加任务失败"
                    }
                }
            } else {
                console.log(time + "：添加项目失败");
                ctx.body = {
                    code: 202,
                    status: "error",
                    data: time + "：添加项目失败"
                }
            }
        } else {
            console.log(time + "：添加用户失败");
            ctx.body = {
                code: 202,
                status: "error",
                data: time + "：添加用户失败"
            }
        }
    } else if (findUser1.toString().length !== 0 && findUser2.toString().length === 0) {    //有发布人但没有收件人
        let userId = findUser1[0]._id.toString()
        let userData2 = {
            access_token: '',
            team: 'cgyun',
            alias: '',
            username: userName2,
            password: '',
            teamName: '',
            teamAlias: '',
            role: '',
            enable: 'true',
            createDate: new Date(),
            photoFileID: '',
            __v: '0'
        }
        let addUserResult2 = await DB.insert('user', userData2);
        let findProjectResult = await DB.find('project', {"userId": userId, "name": projectName})
        let project2 = {
            "name": projectName,
            "userId": addUserResult2.ops[0]._id.toString(),
            "reviews": [],
            "introduction": "欢迎来到我的新项目...",
            "collections": [],
        }
        let addProjectResult2 = await DB.insert('project', project2);
        if (findProjectResult.toString().length === 0) {    //发布人第一次发布此项目
            let project1 = {
                "name": projectName,
                "userId": userId,
                "reviews": [],
                "introduction": "欢迎来到我的新项目...",
                "collections": [],
            }
            let addProjectResult1 = await DB.insert('project', project1);
            if (addProjectResult1.result.ok && addProjectResult2.result.ok) {
                let review = {
                    "name": taskName,
                    "userId": userId,
                    "mediaId": [],
                    "introduction": "任务介绍...",
                    "link": ''
                }
                let addReviewResult1 = await DB.insert('review', review);
                let addReviewResult2 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult1.ops[0]._id.toString())}, {"reviews": addReviewResult1.ops[0]._id})
                let addReviewResult3 = await DB.update("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"link": cryptId(addReviewResult1.ops[0]._id.toString())})
                if (addReviewResult1.result.ok && addReviewResult2.result.ok && addReviewResult3.result.ok) {
                    let addCollectionResult1 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult2.ops[0]._id.toString())}, {"collections": DB.getObjectId(addReviewResult1.ops[0]._id)})
                    let addCollectionResult2 = await DB.addArray('review', {'_id': DB.getObjectId(addReviewResult1.ops[0]._id)}, {'userId': addUserResult2.ops[0]._id.toString()})
                    if (addCollectionResult1.result.ok && addCollectionResult2.result.ok) {
                        let addMediaResult1 = await DB.insert('media',
                            {
                                "userId": userId,
                                "userName": userName1,
                                "fileId": DB.getObjectId(fileID),
                                "name": fileName.split(".")[0],
                                "buffer": '',
                                "type": "image/" + fileName.split(".")[1],
                                "like": 0
                            });
                        let addMediaResult2 = await DB.addArray("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"mediaId": addMediaResult1.ops[0]._id})
                        if (addMediaResult1.result.ok && addMediaResult2.result.ok) {
                            console.log(time + "：文件" + fileName + "记录保存成功")
                            console.log(time + "：添加任务文件成功")
                            ctx.body = {
                                code: 200,
                                status: "success",
                                data: ctx.request.body
                            }
                        } else {
                            console.log(time + "：文件" + fileName + "记录保存失败")
                            ctx.body = {
                                code: 202,
                                status: "error",
                                data: time + "：文件" + fileName + "记录保存失败"
                            }
                        }
                    } else {
                        console.log(time + "：收藏任务失败");
                        ctx.body = {
                            code: 202,
                            status: "error",
                            data: time + "：收藏任务失败"
                        }
                    }
                } else {
                    console.log(time + "：添加任务失败");
                    ctx.body = {
                        code: 202,
                        status: "error",
                        data: time + "：添加任务失败"
                    }
                }
            } else {
                console.log(time + "：添加项目失败");
                ctx.body = {
                    code: 202,
                    status: "error",
                    data: time + "：添加项目失败"
                }
            }
        } else {    //数据库中有此项目
            let reviews = []
            reviews = findProjectResult[0].reviews
            let flag = true
            for (let i = 0; i < reviews.length; i++) {
                let findReviewResult = await DB.find('review', {"_id": reviews[i]})
                let reviewName = findReviewResult[0].name
                if (reviewName === taskName) {
                    flag = false    //有当前任务
                    let addCollectionResult1 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult2.ops[0]._id.toString())}, {"collections": reviews[i]})
                    let addCollectionResult2 = await DB.addArray('review', {'_id': reviews[i]}, {'userId': addUserResult2.ops[0]._id.toString()})
                    let addMediaResult1 = await DB.insert('media',
                        {
                            "userId": userId,
                            "userName": userName1,
                            "fileId": DB.getObjectId(fileID),
                            "name": fileName.split(".")[0],
                            "buffer": '',
                            "type": "image/" + fileName.split(".")[1],
                            "like": 0
                        });
                    let addMediaResult2 = await DB.addArray("review", {"_id": DB.getObjectId(findReviewResult[0]._id)}, {"mediaId": addMediaResult1.ops[0]._id})
                    if (addCollectionResult1.result.ok && addCollectionResult2.result.ok && addMediaResult1.result.ok && addMediaResult2.result.ok) {
                        console.log(time + "：文件" + fileName + "记录保存成功")
                        console.log(time + "：添加任务文件成功")
                        ctx.body = {
                            code: 200,
                            status: "success",
                            data: ctx.request.body
                        }
                    } else {
                        console.log(time + "：文件" + fileName + "记录保存失败")
                        ctx.body = {
                            code: 202,
                            status: "error",
                            data: time + "：文件" + fileName + "记录保存失败"
                        }
                    }
                    break
                }
            }
            if (flag) { //无当前任务
                let review = {
                    "name": taskName,
                    "userId": [userId],
                    "mediaId": [],
                    "introduction": "任务介绍...",
                    "link": ''
                }
                let addReviewResult1 = await DB.insert('review', review);
                let addReviewResult2 = await DB.addArray("project", {"_id": DB.getObjectId(findProjectResult[0]._id.toString())}, {"reviews": addReviewResult1.ops[0]._id})
                let addReviewResult3 = await DB.update("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"link": cryptId(addReviewResult1.ops[0]._id.toString())})
                if (addReviewResult1.result.ok && addReviewResult2.result.ok && addReviewResult3.result.ok) {
                    let addCollectionResult1 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult2.ops[0]._id.toString())}, {"collections": DB.getObjectId(addReviewResult1.ops[0]._id)})
                    let addCollectionResult2 = await DB.addArray('review', {'_id': DB.getObjectId(addReviewResult1.ops[0]._id)}, {'userId': addUserResult2.ops[0]._id.toString()})
                    let addMediaResult1 = await DB.insert('media',
                        {
                            "userId": userId,
                            "userName": userName1,
                            "fileId": DB.getObjectId(fileID),
                            "name": fileName.split(".")[0],
                            "buffer": '',
                            "type": "image/" + fileName.split(".")[1],
                            "like": 0
                        });
                    let addMediaResult2 = await DB.addArray("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"mediaId": addMediaResult1.ops[0]._id})
                    if (addCollectionResult1.result.ok && addCollectionResult2.result.ok && addMediaResult1.result.ok && addMediaResult2.result.ok) {
                        console.log(time + "：文件" + fileName + "记录保存成功")
                        console.log(time + "：添加任务文件成功")
                        ctx.body = {
                            code: 200,
                            status: "success",
                            data: ctx.request.body
                        }
                    } else {
                        console.log(time + "：文件" + fileName + "记录保存失败")
                        ctx.body = {
                            code: 202,
                            status: "error",
                            data: time + "：文件" + fileName + "记录保存失败"
                        }
                    }
                } else {
                    console.log(time + "：添加任务失败");
                    ctx.body = {
                        code: 202,
                        status: "error",
                        data: time + "：添加任务失败"
                    }
                }
            }
        }
    } else if (findUser1.toString().length === 0 && findUser2.toString().length !== 0) {    //没有发布人但有收件人
        let userData1 = {
            access_token: '',
            team: 'cgyun',
            alias: '',
            username: userName1,
            password: '',
            teamName: '',
            teamAlias: '',
            role: '',
            enable: 'true',
            createDate: new Date(),
            photoFileID: '',
            __v: '0'
        }
        let addUserResult1 = await DB.insert('user', userData1);
        let userId = findUser2[0]._id.toString()
        if (addUserResult1.result.ok) {
            let project1 = {
                "name": projectName,
                "userId": addUserResult1.ops[0]._id.toString(),
                "reviews": [],
                "introduction": "欢迎来到我的新项目...",
                "collections": [],
            }
            let project2 = {
                "name": projectName,
                "userId": userId,
                "reviews": [],
                "introduction": "欢迎来到我的新项目...",
                "collections": [],
            }
            let addProjectResult1 = await DB.insert('project', project1);
            let addProjectResult2 = await DB.insert('project', project2);
            if (addProjectResult1.result.ok && addProjectResult2.result.ok) {
                let review = {
                    "name": taskName,
                    "userId": [addUserResult1.ops[0]._id.toString()],
                    "mediaId": [],
                    "introduction": "任务介绍...",
                    "link": ''
                }
                let addReviewResult1 = await DB.insert('review', review);
                let addReviewResult2 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult1.ops[0]._id.toString())}, {"reviews": addReviewResult1.ops[0]._id})
                let addReviewResult3 = await DB.update("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"link": cryptId(addReviewResult1.ops[0]._id.toString())})
                if (addReviewResult1.result.ok && addReviewResult2.result.ok && addReviewResult3.result.ok) {
                    let addCollectionResult1 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult2.ops[0]._id.toString())}, {"collections": DB.getObjectId(addReviewResult1.ops[0]._id)})
                    let addCollectionResult2 = await DB.addArray('review', {'_id': DB.getObjectId(addReviewResult1.ops[0]._id)}, {'userId': userId})
                    if (addCollectionResult1.result.ok && addCollectionResult2.result.ok) {
                        let addMediaResult1 = await DB.insert('media',
                            {
                                "userId": addUserResult1.ops[0]._id.toString(),
                                "userName": userName1,
                                "fileId": DB.getObjectId(fileID),
                                "name": fileName.split(".")[0],
                                "buffer": '',
                                "type": "image/" + fileName.split(".")[1],
                                "like": 0
                            });
                        let addMediaResult2 = await DB.addArray("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"mediaId": addMediaResult1.ops[0]._id})
                        if (addMediaResult1.result.ok && addMediaResult2.result.ok) {
                            console.log(time + "：文件" + fileName + "记录保存成功")
                            console.log(time + "：添加任务文件成功")
                            ctx.body = {
                                code: 200,
                                status: "success",
                                data: ctx.request.body
                            }
                        } else {
                            console.log(time + "：文件" + fileName + "记录保存失败")
                            ctx.body = {
                                code: 202,
                                status: "error",
                                data: time + "：文件" + fileName + "记录保存失败"
                            }
                        }
                    } else {
                        console.log(time + "：收藏任务失败");
                        ctx.body = {
                            code: 202,
                            status: "error",
                            data: time + "：收藏任务失败"
                        }
                    }
                } else {
                    console.log(time + "：添加任务失败");
                    ctx.body = {
                        code: 202,
                        status: "error",
                        data: time + "：添加任务失败"
                    }
                }
            } else {
                console.log(time + "：添加项目失败");
                ctx.body = {
                    code: 202,
                    status: "error",
                    data: time + "：添加项目失败"
                }
            }
        } else {
            console.log(time + "：添加用户失败");
            ctx.body = {
                code: 202,
                status: "error",
                data: time + "：添加用户失败"
            }
        }
    } else {    //有发布人也有收件人
        let userId1 = findUser1[0]._id.toString()
        let userId2 = findUser2[0]._id.toString()
        let findProjectResult1 = await DB.find('project', {"userId": userId1, "name": projectName})
        let findProjectResult2 = await DB.find('project', {"userId": userId2, "name": projectName})
        if (findProjectResult2.toString().length === 0) {   //收件人无此项目
            let project2 = {
                "name": projectName,
                "userId": userId2,
                "reviews": [],
                "introduction": "欢迎来到我的新项目...",
                "collections": [],
            }
            let addProjectResult2 = await DB.insert('project', project2);
        }
        if (findProjectResult1.toString().length !== 0) {   //发布人有此项目
            let findProjectResult2 = await DB.find('project', {"userId": userId2, "name": projectName})
            let reviews = []
            reviews = findProjectResult1[0].reviews
            let flag = true
            for (let i = 0; i < reviews.length; i++) {
                let findReviewResult = await DB.find('review', {"_id": reviews[i]})
                let reviewName = findReviewResult[0].name
                if (reviewName === taskName) {
                    flag = false    //有当前任务

                    let collections = findProjectResult2[0].collections
                    let flag2 = collections.some(function (x) {
                        return x === reviews[i]
                    })
                    if (!flag2) {   //收件人的项目中没有收藏该任务
                        let addCollectionResult1 = await DB.addArray("project", {"_id": findProjectResult2[0]._id}, {"collections": reviews[i]})
                        let addCollectionResult2 = await DB.addArray('review', {'_id': reviews[i]}, {'userId': userId2})
                    }

                    let addMediaResult1 = await DB.insert('media',
                        {
                            "userId": userId1,
                            "userName": userName1,
                            "fileId": DB.getObjectId(fileID),
                            "name": fileName.split(".")[0],
                            "buffer": '',
                            "type": "image/" + fileName.split(".")[1],
                            "like": 0
                        });
                    let addMediaResult2 = await DB.addArray("review", {"_id": DB.getObjectId(findReviewResult[0]._id)}, {"mediaId": addMediaResult1.ops[0]._id})
                    if (addMediaResult1.result.ok && addMediaResult2.result.ok) {
                        console.log(time + "：文件" + fileName + "记录保存成功")
                        console.log(time + "：添加任务文件成功")
                        ctx.body = {
                            code: 200,
                            status: "success",
                            data: ctx.request.body
                        }
                    } else {
                        console.log(time + "：文件" + fileName + "记录保存失败")
                        ctx.body = {
                            code: 202,
                            status: "error",
                            data: time + "：文件" + fileName + "记录保存失败"
                        }
                    }
                    break
                }
            }
            if (flag) { //无当前任务
                let review = {
                    "name": taskName,
                    "userId": [userId1, userId2],
                    "mediaId": [],
                    "introduction": "任务介绍...",
                    "link": ''
                }
                let addReviewResult1 = await DB.insert('review', review);
                let addReviewResult2 = await DB.addArray("project", {"_id": DB.getObjectId(findProjectResult1[0]._id.toString())}, {"reviews": addReviewResult1.ops[0]._id})
                let addReviewResult3 = await DB.update("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"link": cryptId(addReviewResult1.ops[0]._id.toString())})
                if (addReviewResult1.result.ok && addReviewResult2.result.ok && addReviewResult3.result.ok) {
                    let addCollectionResult1 = await DB.addArray("project", {"_id": DB.getObjectId(findProjectResult2[0]._id.toString())}, {"collections": DB.getObjectId(addReviewResult1.ops[0]._id)})
                    let addMediaResult1 = await DB.insert('media',
                        {
                            "userId": userId1,
                            "userName": userName1,
                            "fileId": DB.getObjectId(fileID),
                            "name": fileName.split(".")[0],
                            "buffer": '',
                            "type": "image/" + fileName.split(".")[1],
                            "like": 0
                        });
                    let addMediaResult2 = await DB.addArray("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"mediaId": addMediaResult1.ops[0]._id})
                    if (addCollectionResult1.result.ok && addMediaResult1.result.ok && addMediaResult2.result.ok) {
                        console.log(time + "：文件" + fileName + "记录保存成功")
                        console.log(time + "：添加任务文件成功")
                        ctx.body = {
                            code: 200,
                            status: "success",
                            data: ctx.request.body
                        }
                    } else {
                        console.log(time + "：文件" + fileName + "记录保存失败")
                        ctx.body = {
                            code: 202,
                            status: "error",
                            data: time + "：文件" + fileName + "记录保存失败"
                        }
                    }
                } else {
                    console.log(time + "：添加任务失败");
                    ctx.body = {
                        code: 202,
                        status: "error",
                        data: time + "：添加任务失败"
                    }
                }
            }
        } else {    //发布人无此项目
            let project1 = {
                "name": projectName,
                "userId": userId1,
                "reviews": [],
                "introduction": "欢迎来到我的新项目...",
                "collections": [],
            }
            let project2 = {
                "name": projectName,
                "userId": userId2,
                "reviews": [],
                "introduction": "欢迎来到我的新项目...",
                "collections": [],
            }
            let addProjectResult1 = await DB.insert('project', project1);
            let addProjectResult2 = await DB.insert('project', project2);
            if (addProjectResult1.result.ok && addProjectResult2.result.ok) {
                let review = {
                    "name": taskName,
                    "userId": [userId1],
                    "mediaId": [],
                    "introduction": "任务介绍...",
                    "link": ''
                }
                let addReviewResult1 = await DB.insert('review', review);
                let addReviewResult2 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult1.ops[0]._id.toString())}, {"reviews": addReviewResult1.ops[0]._id})
                let addReviewResult3 = await DB.update("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"link": cryptId(addReviewResult1.ops[0]._id.toString())})
                if (addReviewResult1.result.ok && addReviewResult2.result.ok && addReviewResult3.result.ok) {
                    let addCollectionResult1 = await DB.addArray("project", {"_id": DB.getObjectId(addProjectResult2.ops[0]._id.toString())}, {"collections": DB.getObjectId(addReviewResult1.ops[0]._id)})
                    let addCollectionResult2 = await DB.addArray('review', {'_id': DB.getObjectId(addReviewResult1.ops[0]._id)}, {'userId': userId2})
                    if (addCollectionResult1.result.ok && addCollectionResult2.result.ok) {
                        let addMediaResult1 = await DB.insert('media',
                            {
                                "userId": userId1,
                                "userName": userName1,
                                "fileId": DB.getObjectId(fileID),
                                "name": fileName.split(".")[0],
                                "buffer": '',
                                "type": "image/" + fileName.split(".")[1],
                                "like": 0
                            });
                        let addMediaResult2 = await DB.addArray("review", {"_id": DB.getObjectId(addReviewResult1.ops[0]._id)}, {"mediaId": addMediaResult1.ops[0]._id})
                        if (addMediaResult1.result.ok && addMediaResult2.result.ok) {
                            console.log(time + "：文件" + fileName + "记录保存成功")
                            console.log(time + "：添加任务文件成功")
                            ctx.body = {
                                code: 200,
                                status: "success",
                                data: ctx.request.body
                            }
                        } else {
                            console.log(time + "：文件" + fileName + "记录保存失败")
                            ctx.body = {
                                code: 202,
                                status: "error",
                                data: time + "：文件" + fileName + "记录保存失败"
                            }
                        }
                    } else {
                        console.log(time + "：收藏任务失败");
                        ctx.body = {
                            code: 202,
                            status: "error",
                            data: time + "：收藏任务失败"
                        }
                    }
                } else {
                    console.log(time + "：添加任务失败");
                    ctx.body = {
                        code: 202,
                        status: "error",
                        data: time + "：添加任务失败"
                    }
                }
            } else {
                console.log(time + "：添加项目失败");
                ctx.body = {
                    code: 202,
                    status: "error",
                    data: time + "：添加项目失败"
                }
            }
        }
    }
});

function cryptId(id) {
    let md5 = crypto.createHash('md5');
    return md5.update(id).digest('hex');
}


module.exports = router.routes();